package com.lse_api.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>此类用于获取文件夹下下文件并对其操作</p>
 *
 * @author bcl
 */
public class FileTree {


    /**
     * 文件夹目录储存
     */
    private static List<String> folderName = null;

    /**
     * 文件储存
     */
    private static List<String> fileName = null;


    /**
     * <p>获取路径下文件结构</p>
     *
     * @param path 路径
     * @return
     */
    public static Map<String, List<String>> getTree(String path) {
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        File file = new File(path);
        folderName = new ArrayList<String>();
        fileName = new ArrayList<String>();
        if (!file.exists()) {
            map.put("folder", folderName);
            map.put("file", fileName);
            return map;
        }
        File[] chiled_file = file.listFiles();
        if (chiled_file == null || chiled_file.length == 0) {
            map.put("folder", folderName);
            map.put("file", fileName);
            return map;
        }
        for (int i = 0; i < chiled_file.length; i++) {
            File f = chiled_file[i];
            // 判断是文件夹还是文件
            if (!f.isHidden()) {
                if (f.isDirectory()) {
                    folderName.add(f.getName());
                } else {
                    fileName.add(f.getName());
                }
            }
        }
        map.put("folder", folderName);
        map.put("file", fileName);
        return map;
    }

    /**
     * <p>创建文件夹<p>
     *
     * @param path 创建路径  控制层将要创建的文件夹名(公司名or岗位名)拼接在一起
     * @return
     */
    public static boolean addFolder(String path) {
        File file = new File(path);
        boolean flag = false;
        if (!file.exists()) {
            flag = file.mkdirs();
            return flag;
        }
        if (file.isDirectory()) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * <p>清除字符串中的制表符 空格 换行符等</p>
     *
     * @param string
     * @return
     */
    public static String replaceBlank(String string) {
        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher(string);
        return m.replaceAll("");
    }

    /**
     * <p>根据路径删除文件夹或文件</p>
     *
     * @param path
     * @return
     */
    public static boolean deleteFolderOrFile(String path) {
        File file = new File(path);
        File[] files = file.listFiles();
        boolean flag = false;
        if (files == null || files.length == 0) {
            flag = file.delete();
        } else {
            flag = delFolder(path);
        }
        return flag;
    }

    /**
     * <p>删除文件夹</p>
     *
     * @param folderPath 文件夹完整绝对路径
     * @return
     */
    public static boolean delFolder(String folderPath) {
        try {
            boolean flag = delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete(); //删除空文件夹
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * <p>删除指定文件夹下所有文件</p>
     *
     * @param path 文件夹完整绝对路径
     * @return
     */
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        String path = "F:\\test/Android_SDK_V3.3.3 - 副本";
        System.out.println(deleteFolderOrFile(path));
    }

}
